package com.doge.walknovel.api

import com.doge.walknovel.core.account.AccountHelper
import com.doge.walknovel.core.account.AppUserViewModel
import com.doge.walknovel.utils.runOnUI

object ErrorHandler {
    private const val HTTP_TOKEN_VALID = 1502
    private const val HTTP_SUCCESS = 200
    const val HTTP_NO_MONEY = 602

    fun process(resp: ApiResult<*>?) {
        when {
            resp == null -> {
                throw ServerException(-999, "resp null pointer")
            }
            resp.code == HTTP_TOKEN_VALID -> runOnUI {
                AccountHelper.updateUToken(null)
                AppUserViewModel.get().updateSelf(null)
                AccountHelper.requiredLogin()
            }
            resp.code != HTTP_SUCCESS -> {
                throw ServerException(resp.code, resp.message ?: "")
            }
        }
    }
}